﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
	protected void btnSend_Click(object sender, EventArgs e)
	{
		try
		{
			SmtpClient smtp = new SmtpClient
			{
				Host = txtHost.Text,
				Port = int.Parse(txtPort.Text),
				EnableSsl = chkEnabledSsl.Checked,
				Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text),
			};

			MailAddress from = new MailAddress(
				txtAddress.Text,
				txtDisplayName.Text,
				Encoding.GetEncoding(txtEncoding.Text));

			MailAddress to = new MailAddress(txtTo.Text);

			MailMessage msg = new MailMessage(from, to)
			{
				Subject = txtSubject.Text,
				Body = txtBody.Text,
				SubjectEncoding = Encoding.GetEncoding(txtEncoding.Text),
				BodyEncoding = Encoding.GetEncoding(txtEncoding.Text),
				IsBodyHtml = true,
			};

			smtp.Send(msg);

			lblMsg.Text = "发送成功。";
		}
		catch(Exception ex)
		{
			lblMsg.Text = "错误：" + HttpUtility.HtmlEncode(ex.Message);
		}
	}
}
